// pages/address/chooseAddress/chooseAddress.js
var http = require('../../../utils/server.js');
var util = require('../../../utils/util.js');
Page({

  data: {
    addressList: [],
    selected: [],
    choose: true
  },

  onLoad: function(option) {
    var that = this
    //列表，隐藏选择
    if (option.list) {
      that.setData({
        choose: false
      });
      wx.setNavigationBarTitle({
        title: '收货地址'
      })
    } else {
      wx.setNavigationBarTitle({
        title: '选择地址'
      })
    }
    that.getData()
  },
  getData: function() {
    var that = this
    http.post({
      url: "address/getAll"
    }, function(data) {
      console.log('result', data)
      that.setData({
        addressList: data
      })
    })
  },
  confirm: function() {
    var pages = getCurrentPages();
    if (pages.length > 1) {
      var prePage = pages[pages.length - 2];
      prePage.chooseAddress(this.data.selected)
    }
    wx.navigateBack({
      delta: 1
    })
  },
  // 暂时只开发单商品单地址 checkboxChange变radioChange
  radioChange: function(e) {
    this.setData({
      selected: e.detail.value
    })
  },
  editAddress(e) {
    var item = e.currentTarget.dataset.item
    console.log(util.urlencode(item))
    wx.navigateTo({
      url: '/pages/address/editAddress/editAddress?' + util.urlencode(item)
    })
  }

})